/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { assert, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { ComponentDegreeCheck } from '../../../src/checker/performance/ComponentDegreeCheck';
import { ComponentTreeLayersCheck } from '../../../src/checker/performance/ComponentTreeLayersCheck';
import { ComponentCountCheck, setCount } from '../../../src/checker/performance/ComponentCountCheck';


describe('ComponentLimitCheckTest', () => {

    test('ComponentLimitCheckTest_001', async () => {
        const rule: Rule = new Rule('');
        let checkEntry = await testCaseCheck('./test/unittest/sample/ComponentLimit', rule, CHECK_MODE.PROJECT2CHECK, ComponentDegreeCheck);
        let realPath = checkEntry.scene.getRealProjectDir();
        const detectFile: string = path.join(realPath, 'ets', 'ComponentDegreeReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => (issue.defect.mergeKey.startsWith(detectFile)));
        let expectReports = detectFileReports.filter(issue => issue.defect.fixKey.includes('23%5%8%'));
        expect(expectReports.length).toBe(1);
    });

    test('ComponentLimitCheckTest_002', async () => {
        const rule: Rule = new Rule('');
        let checkEntry = await testCaseCheck('./test/unittest/sample/ComponentLimit', rule, CHECK_MODE.PROJECT2CHECK, ComponentTreeLayersCheck);
        let realPath = checkEntry.scene.getRealProjectDir();
        const detectFile: string = path.join(realPath, 'ets', 'ComponentLayersReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => (issue.defect.mergeKey.startsWith(detectFile)));
        expect(detectFileReports.length).toBe(2);
        const expectReportList = ['53%125%128%', '54%129%132%'];
        detectFileReports.forEach((issue, index) => {
            assert.include(issue.defect.fixKey, expectReportList[index]);
        });
    })

    test('ComponentLimitCheckTest_003', async () => {
        const rule: Rule = new Rule('');
        setCount(10);
        let checkEntry = await testCaseCheck('./test/unittest/sample/ComponentLimit', rule, CHECK_MODE.PROJECT2CHECK, ComponentCountCheck);
        let realPath = checkEntry.scene.getRealProjectDir();
        const detectFile: string = path.join(realPath, 'ets', 'ComponentCountReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => (issue.defect.mergeKey.startsWith(detectFile)));
        let expectReports = detectFileReports.filter(issue => issue.defect.fixKey.includes('23%5%8%'));
        expect(expectReports.length).toBe(1);
    })
})